<?php

namespace App\Http\Repository;

use App\Video;
use App\VideoChannel;
use Illuminate\Support\Facades\DB;
class VideoRepository
{
    /**
     * @param string $video_channel_categroy_name
     * @param int $limit
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getListByVideoChannelCategoryName(string $video_channel_categroy_name, int $limit = 15)
    {
        $paginate = DB::table('videos as v')
            ->leftJoin('video_channels as vc', 'v.video_channel_id', '=', 'vc.id')
            ->leftJoin('video_channel_categorys as vcc',
                'vc.video_channel_category_id',
                '=',
                'vcc.id')
            ->where('vcc.name', $video_channel_categroy_name)
            ->select([
                "v.id",
                "v.name",
                "v.url",
                'v.updated_at',
                "v.pic_url",
                "v.video_channel_id",
                "vc.name as video_channel_name",
                "vcc.name as video_channel_category_name"
            ])
            ->orderBy('v.updated_at', 'desc')
            ->paginate($limit);

        return $paginate;
    }

    /**
     * @param int $channel_id
     * @param int $limit
     * @return mixed
     */
    public function getListByChannelId(int $channel_id, int $limit = 15)
    {
        $paginate = Video::where('video_channel_id', $channel_id)
            ->orderBy('updated_at', 'desc')
            ->paginate($limit);

        return $paginate;
    }

    /**
     * @param int $id
     * @return Video|Video[]|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     */
    public function show(int $id)
    {
        $data = Video::with('videoChannel')->find($id);
        return $data;
    }
}
